'use client';

import type { RootState } from '@/store';
import { useGlobalStateHook } from './useGlobalStateHook';

type ModuleName = keyof RootState;

export function useGlobalState<
  M extends ModuleName,
  // 当没有传入key参数时，泛型变量没有被推导，类型约束后增加默认类型为undefined
  // 这样 Fd 会被推导为 undefined 往下传
  Fd extends
    | undefined
    | Exclude<keyof RootState[M], number | symbol> = undefined,
>(module: M, key?: Fd) {
  // type T1 = Fd extends undefined ? 'uu' : Fd
  // return true as unknown as T1

  // @ts-ignore
  // 当 Fd 被推导为undefined时，不能作为 R[M] 对象的键，进一步提取类型
  return useGlobalStateHook<ModuleName, RootState, M, Fd>(module, key);
}
